const {
  $util,
} = getApp();
Page({
  data: {
    page: 1,
    isloadmore: true,
    seclist: [],
    comment: [],
    commentTxt: '',
    active: 0,
  },
  onLoad(options) {
    this.setData(options);
  },
  onShow() {
    this.loadDetail();
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
  },
  changenav(e) {
    const {
      idx,
    } = e.currentTarget.dataset;
    this.setData({
      active: Number(idx),
      page: 1,
      isCLoad: false,
      isSLoad: false,
    }, () => {
      Number(idx) === 1 && this.loadSecList();
      Number(idx) === 2 && this.loadComment();
    });
  },
  /**
   * 课程详情
   */
  loadDetail() {
    $util.request({
      url: 'entry/wxapp/edu.train.getlessonsdetails',
      data: {
        id: this.data.id,
      },
      success: (res) => {
        res.list.page = 1;
        this.setData(res.list, () => {
          this.loadSecList();
        });
        // 网络党校 -- 学习课程
        // wx.setNavigationBarTitle({
        //   title: res.list.name || '',
        // });
        wx.setNavigationBarTitle({
          title: '网络课程',
        });
      },
    });
  },
  loadSecList() {
    let {
      seclist,
      page,
      id,
    } = this.data;
    $util.request({
      url: 'entry/wxapp/edu.train.getseclist',
      data: {
        id: id,
        page: page,
      },
      success: (res) => {
        let list = res.seclist;
        if (page > 1) {
          list = [...seclist, ...list];
        }
        this.setData({
          seclist: list,
          isloadmore: seclist.length < Number(res.sectotal),
          page: seclist.length < Number(res.sectotal) ? page + 1 : page,
          isSLoad: true,
        });
      },
    });
  },
  bindIputval(e) {
    this.setData({
      commentTxt: e.detail.value,
    });
  },
  loadComment() {
    let {
      comment,
      page,
      id,
    } = this.data;
    $util.request({
      url: 'entry/wxapp/edu.train.lessonTopics',
      data: {
        onlineid: id,
        page: page,
      },
      method: 'GET',
      success: (res) => {
        let list = res.list;
        if (page > 1) {
          list = [...comment, ...list];
        }
        this.setData({
          comment: list,
          isloadmore: res.list.length > 0,
          page: page + 1,
          isCLoad: true,
        });
      },
    });
  },
  isprevent: false,
  addComment() {
    let {
      comment,
      commentTxt,
      id,
    } = this.data;
    if (!commentTxt.trim()) {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
      });
      return;
    }
    if (this.isprevent) return;
    this.isprevent = true;
    $util.request({
      url: 'entry/wxapp/edu.train.createTopic',
      data: {
        onlineid: id,
        content: commentTxt,
      },
      success: (res) => {
        wx.showToast({
          title: '评论成功',
          icon: 'success',
        });
        comment = [...res.list, ...comment];
        this.setData({
          commentTxt: '',
          comment: comment,
        });
      },
      complete: () => {
        this.isprevent = false;
      },
    });
  },
  onReachBottom() {
    this.data.active === 1 && this.data.isloadmore && this.loadSecList();
    this.data.active === 2 && this.data.isloadmore && this.loadComment();
  },
  onShareAppMessage() {
    let _this = this;
    let path = '/ws_party_const/pages/education/train/network/detail/index?id' + _this.data.id;
    let res = {
      path,
    };
    return res;
  },
});